package com.zyx.camunda.config;

import com.zyx.camunda.helpers.listener.CustomBpmnParseListener;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.cfg.ProcessEnginePlugin;
import org.camunda.bpm.spring.boot.starter.event.EventPublisherPlugin;
import org.camunda.bpm.spring.boot.starter.property.EventingProperty;
import org.springframework.context.ApplicationEventPublisher;

/**
 * <a href="http://camunda-cn.shaochenfeng.com/user-guide/spring-boot-integration/configuration/">自定义camunda配置</a>
 * 参考 {@link EventPublisherPlugin} 的实现
 *
 * @author Yaxi.Zhang
 * @since 2022/10/18 01:00
 */
public class MyCustomConfiguration implements ProcessEnginePlugin {

    private final EventingProperty property;
    private final ApplicationEventPublisher publisher;

    public MyCustomConfiguration(final EventingProperty property, final ApplicationEventPublisher publisher) {
        this.publisher = publisher;
        this.property = property;
    }

    @Override
    public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
        // 开启 Job执行器 camunda.bpm.job-execution.enabled
        // processEngineConfiguration.setJobExecutorActivate(true);
        // 设置日志级别，默认为full
        // processEngineConfiguration.setHistory(ProcessEngineConfiguration.HISTORY_FULL);
        // 是否允许数据库批处理，低版本oracle需要注意该配置项
        // processEngineConfiguration.setJdbcBatchProcessing(false);
        // 自定义 BpmnParse 工厂
        // processEngineConfiguration.setBpmnParseFactory(new CustomBpmnParseFactory());
        //processEngineConfiguration.setEventHandlers(new HashMap<String, EventHandler>());

        // 添加监听器
        if (property.isTask() || property.isExecution()) {
            processEngineConfiguration.getCustomPostBPMNParseListeners().add(new CustomBpmnParseListener(this.publisher, property));
        }

    }

    @Override
    public void postInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
    }

    @Override
    public void postProcessEngineBuild(ProcessEngine processEngine) {

    }

}
